﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.SqlServer.Repositories.Readers
{
	using System.Data.SqlClient;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Enums;

	public class UserReader : IReader<User>
	{
		/// <summary>
		/// Populates the specified reader.
		/// </summary>
		/// <param name="reader">The reader.</param>
		/// <param name="entity">The entity.</param>
		public void Populate(SqlDataReader reader, User entity)
		{
			// user
			entity.Id = reader.GetInt32(0);
			entity.Name = reader.GetString(1);
			entity.Slug = reader.GetString(2);
			entity.EmailAddress = reader.GetString(3);
			entity.Posts = reader.GetInt32(4);
			entity.Reputation = reader.GetInt32(5);
			entity.Status = (UserStatus)reader.GetInt32(6);
			entity.Created = reader.GetDateTime(7);
			entity.Updated = reader.GetDateTime(8);
			entity.LastVisit = reader.GetDateTime(9);
			entity.LastLogOn = reader.GetDateTime(10);

			// user preference
			entity.Preference = new UserPreference
			{
				Id = reader.GetInt32(11),
				TimeFormat = (UserPreferenceTimeFormat)reader.GetInt32(12),
				Country = new Country
				{
					Id = reader.GetInt32(13),
					Title = reader.GetString(14),
					Slug = reader.GetString(15),
					Code = reader.GetString(16)
				},
				TimeZone = new TimeZone
				{
					Id = reader.GetInt32(17),
					Title = reader.GetString(18),
					Slug = reader.GetString(19),
					Display = reader.GetString(20),
					Offset = reader.GetDouble(21)
				}
			};

			// user profile
			entity.Profile = new UserProfile
         	{
				Id = reader.GetInt32(22), 
				Title = reader.GetString(23)
         	};

			if (reader.IsDBNull(24))
			{
				return;
			}

			// user avatar
			entity.Profile.Avatar = new UserAvatar
        	{
				Id = reader.GetInt32(24),
        		FileName = reader.GetString(25),
				FileDiskName = reader.GetString(26),
        		FilePath = reader.GetString(27),
				FileType = reader.GetString(28)
        	};
		}
	}
}